﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Windows.Forms;

namespace TeachMe.Application.Forms
{
    public class PseudoModalForm : Form
    {
        private PseudoModalForm _pseudoParent;
        private PseudoModalForm _pseudoChild;

        public PseudoModalForm PseudoParent
        {
            get
            {
                return _pseudoParent;
            }
            set
            {
                _pseudoParent = value;

                if (_pseudoParent != null)
                {
                    ShowInTaskbar = false;

                    _pseudoParent._pseudoChild = this;
                    _pseudoParent.Enabled = false;

                    PseudoParentSet(_pseudoParent);
                }
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            if (_pseudoParent != null)
            {
                _pseudoParent._pseudoChild = null;
                _pseudoParent.Enabled = true;

                _pseudoParent.BringToFront();
            }

            base.OnClosed(e);
        }

        protected virtual void PseudoParentSet(PseudoModalForm pseudoParent)
        {
            //
        }

        protected override void OnActivated(EventArgs e)
        {
            if (_pseudoChild != null)
            {
                _pseudoChild.Activate();
            }

            base.OnActivated(e);
        }

        public new IButtonControl AcceptButton
        {
            get
            {
                return base.AcceptButton;
            }
            set
            {
                if (value is Button)
                {
                    ((Button) value).Click += delegate
                                                  {
                                                      DialogResult = DialogResult.OK;
                                                      Close();
                                                  };
                }

                base.AcceptButton = value;
            }
        }

        public new IButtonControl CancelButton
        {
            get
            {
                return base.CancelButton;
            }
            set
            {
                if (value is Button)
                {
                    ((Button)value).Click += delegate
                    {
                        DialogResult = DialogResult.Cancel;
                        Close();
                    };
                }

                base.CancelButton = value;
            }
        }
    }
}
